package clientView;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import object.TSM_Dep;
import utility.StringUtil;
import clientMain.Main;
import clientUtilGUI.UtilityGUI;
import database.TotalSalaryDelegate;

public class SS_List extends JPanel implements ActionListener{

	private int depId;
	private String depName;
	private String monthYear;
	private int pageCur = 1;
	private JPanel panelHeader;
	private JPanel panelBody;
	private JScrollPane scrollPane;
	private JPanel panelPage;
	private JButton btnBack;
	private JComboBox cbbPage;
	private JButton btnNext;	
	private static JTable tblListSSInDep;
	private JLabel lblMonthYear;
	private JLabel lblDep;
	private JLabel lblMY;
	private JLabel lblDepName;
	private JLabel lblTotalS;
	private JLabel lblTS;

	public SS_List(int _depId, String _depName, String _monthYear) {
		depId = _depId;
		depName = _depName;
		monthYear = _monthYear;
		TotalSalaryDelegate tsd = new TotalSalaryDelegate();
		Vector<TSM_Dep> tsmDeps = tsd.getTSM_Dep(0, Integer.toString(depId), monthYear, monthYear, true);
		String money = StringUtil.formatMoney(Integer.toString(tsmDeps.get(0).getTotalSalary())) + " VNĐ";
		
		setLayout(new BorderLayout(0, 0));
		panelHeader = UtilityGUI.headerForm("Bảng Lương");
		add(panelHeader, BorderLayout.NORTH);
		
		panelBody = new JPanel();
		GridBagLayout gbl_panelBody = new GridBagLayout();
		gbl_panelBody.columnWidths = new int[]{0, 0, 0, 0, 0, 0};
		gbl_panelBody.rowHeights = new int[]{0, 0, 0};
		gbl_panelBody.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
		gbl_panelBody.rowWeights = new double[]{0.0, 1.0, 0.0};
		panelBody.setLayout(gbl_panelBody);
		add(panelBody, BorderLayout.CENTER);
		
		tblListSSInDep = UtilityGUI.getTblListSSinDep(monthYear, depId, pageCur, "TotalSalary", true);
		tblListSSInDep.addMouseListener(new Mouse_tblSSDep());
		
		lblMonthYear = new JLabel("Tháng :");
		lblMonthYear.setFont(UtilityGUI.fontHeader2);		
		GridBagConstraints gbc_lblMonthYear = new GridBagConstraints();
		gbc_lblMonthYear.anchor = GridBagConstraints.EAST;
		gbc_lblMonthYear.insets = new Insets(5, 10, 5, 5);
		gbc_lblMonthYear.gridx = 0;
		gbc_lblMonthYear.gridy = 0;
		panelBody.add(lblMonthYear, gbc_lblMonthYear);
		
		lblMY = new JLabel(monthYear);
		lblMY.setFont(UtilityGUI.fontHeader2);
		lblMY.setForeground(UtilityGUI.colorHeader2);
		GridBagConstraints gbc_lblMY = new GridBagConstraints();
		gbc_lblMY.anchor = GridBagConstraints.WEST;
		gbc_lblMY.insets = new Insets(5, 0, 5, 5);
		gbc_lblMY.gridx = 1;
		gbc_lblMY.gridy = 0;
		panelBody.add(lblMY, gbc_lblMY);
		
		lblDep = new JLabel("Phòng :");
		lblDep.setFont(UtilityGUI.fontHeader2);
		GridBagConstraints gbc_lblDep = new GridBagConstraints();
		gbc_lblDep.anchor = GridBagConstraints.WEST;
		gbc_lblDep.insets = new Insets(5, 15, 5, 5);
		gbc_lblDep.gridx = 2;
		gbc_lblDep.gridy = 0;
		panelBody.add(lblDep, gbc_lblDep);
		
		lblDepName = new JLabel(depName);
		lblDepName.setFont(UtilityGUI.fontHeader2);
		lblDepName.setForeground(UtilityGUI.colorHeader2);
		GridBagConstraints gbc_lblDepName = new GridBagConstraints();
		gbc_lblDepName.anchor = GridBagConstraints.WEST;
		gbc_lblDepName.insets = new Insets(5, 0, 5, 5);
		gbc_lblDepName.gridx = 3;
		gbc_lblDepName.gridy = 0;
		panelBody.add(lblDepName, gbc_lblDepName);
		
		lblTotalS = new JLabel("Tổng Lương :");
		lblTotalS.setFont(UtilityGUI.fontHeader2);
		GridBagConstraints gbc_lblTotalS = new GridBagConstraints();
		gbc_lblTotalS.anchor = GridBagConstraints.WEST;
		gbc_lblTotalS.insets = new Insets(5, 15, 5, 5);
		gbc_lblTotalS.gridx = 4;
		gbc_lblTotalS.gridy = 0;
		panelBody.add(lblTotalS, gbc_lblTotalS);
		
		lblTS = new JLabel(money);
		lblTS.setFont(UtilityGUI.fontHeader2);
		lblTS.setForeground(UtilityGUI.colorHeader2);
		GridBagConstraints gbc_lblTS = new GridBagConstraints();
		gbc_lblTS.anchor = GridBagConstraints.WEST;
		gbc_lblTS.insets = new Insets(5, 0, 5, 10);
		gbc_lblTS.gridx = 5;
		gbc_lblTS.gridy = 0;
		panelBody.add(lblTS, gbc_lblTS);

		scrollPane = new JScrollPane();		
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.insets = new Insets(0, 10, 5, 10);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridwidth = 6;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 1;
		scrollPane.setViewportView(tblListSSInDep);
		panelBody.add(scrollPane, gbc_scrollPane);
		
		panelPage = new JPanel();
		GridBagConstraints gbc_panelPage = new GridBagConstraints();
		gbc_panelPage.gridwidth = 6;
		gbc_panelPage.insets = new Insets(0, 10, 10, 10);
		gbc_panelPage.fill = GridBagConstraints.BOTH;
		gbc_panelPage.gridx = 0;
		gbc_panelPage.gridy = 2;
		panelBody.add(panelPage, gbc_panelPage);
		panelPage.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		btnBack = new JButton(UtilityGUI.iconBack);
		btnBack.addActionListener(this);
		panelPage.add(btnBack);
		
		cbbPage = UtilityGUI.cbbPaging(monthYear, depId);
		cbbPage.addActionListener(this);
		panelPage.add(cbbPage);
		
		btnNext = new JButton(UtilityGUI.iconNext);
		btnNext.addActionListener(this);
		panelPage.add(btnNext);
	}

	public static class Mouse_tblSSDep extends MouseAdapter{
		public void mouseClicked(MouseEvent e) {
			int row = tblListSSInDep.getSelectedRow();
			String emId  = (String) tblListSSInDep.getModel().getValueAt(row, 0);
			if(e.isControlDown() && e.getButton() == e.BUTTON1){
				int index = Main.jTabbedPane.positionTab(UtilityGUI.tabEmInfo);
				if(index == -1){
					Main.jTabbedPane.addTab(new Employee_View(emId), UtilityGUI.tabEmInfo);
					Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
				}else{
					Main.jTabbedPane.removeTabAt(index);
					Main.jTabbedPane.addTab(new Employee_View(emId), UtilityGUI.tabEmInfo, index);
					Main.jTabbedPane.setSelectedIndex(index);
					return;
				}
			}
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnBack){
			if(pageCur == 1){
				return;
			}
			tblListSSInDep.setVisible(false);
			pageCur = pageCur - 1;
			cbbPage.setSelectedItem(pageCur);
			tblListSSInDep = UtilityGUI.getTblListSSinDep(monthYear, depId, pageCur, "TotalSalary", true);
			tblListSSInDep.addMouseListener(new Mouse_tblSSDep());
			scrollPane.setViewportView(tblListSSInDep);
		}else if(e.getSource() == btnNext){
			if(pageCur == cbbPage.getItemCount()){
				return;
			}
			tblListSSInDep.setVisible(false);
			pageCur = pageCur + 1;
			cbbPage.setSelectedItem(pageCur);
			tblListSSInDep = UtilityGUI.getTblListSSinDep(monthYear, depId, pageCur, "TotalSalary", true);
			tblListSSInDep.addMouseListener(new Mouse_tblSSDep());
			scrollPane.setViewportView(tblListSSInDep);
		}else if(e.getSource() == cbbPage){
			int pageGo = (Integer) cbbPage.getSelectedItem();
			if(pageCur == pageGo){
				return;
			}else{
				pageCur = pageGo;
				tblListSSInDep.setVisible(false);
				tblListSSInDep = UtilityGUI.getTblListSSinDep(monthYear, depId, pageCur, "TotalSalary", true);
				tblListSSInDep.addMouseListener(new Mouse_tblSSDep());
				scrollPane.setViewportView(tblListSSInDep);
			}
		}
	}

}
